/*
* Copyright (C) 2008-2009 J-P Nurmi jpnurmi@gmail.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* $Id: viewhandler.cpp 587 2009-05-19 20:19:17Z jpnurmi $
*/

#include "viewhandler.h"
#include "messageview.h"

#if 0
ViewHandler::ViewHandler()
{
}

void ViewHandler::addView(MessageView* view)
{
    d.views.insert(view->receiver().toLower(), view);
}

void ViewHandler::removeView(MessageView* view)
{
    view = d.views.take(view->receiver().toLower());
    if (view)
        view->deleteLater();
}

MessageView* ViewHandler::takeView(const QString& receiver)
{
    return d.views.take(receiver.toLower());
}

void ViewHandler::renameView(const QString& receiver, const QString& newReceiver)
{
    MessageView* view = d.views.take(receiver.toLower());
    d.views[newReceiver.toLower()] = view;
    if (view)
        view->setReceiver(newReceiver);
}

QStringList ViewHandler::channels() const
{
    QStringList channels;
    foreach (const QString& receiver, d.views.keys())
    {
        if (receiver.startsWith("#") || receiver.startsWith("&"))
            channels << receiver;
    }
    return channels;
}

QStringList ViewHandler::receivers() const
{
    return d.views.keys();
}

bool ViewHandler::contains(const QString& receiver) const
{
    return d.views.contains(receiver.toLower());
}

QList<MessageView*> ViewHandler::views() const
{
    return d.views.values();
}

MessageView* ViewHandler::view(const QString& receiver) const
{
    return d.views.value(receiver.toLower());
}

QList<MessageView*> ViewHandler::findViews(const QStringList& receivers, const QStringList& participants) const
{
    QList<MessageView*> views;
    foreach (MessageView* view, d.views)
    {
        bool matches = false;
        foreach (const QString& receiver, receivers)
        {
            matches |= view->matches(receiver);
            if (matches)
                break;
        }

        bool contains = false;
        foreach (const QString& participant, participants)
        {
            contains |= view->contains(participant);
            if (contains)
                break;
        }

        if (matches || contains)
            views << view;
    }
    return views;
}
#endif // 0
